﻿using Generator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ALDIOrganizer
{
    public static class BoolArrayExtensionMethods
    {
        public static byte ToByte(this bool[] pBoolArray)
        {
            var _result = 0;
            foreach (var _bit in pBoolArray)            
                _result = (_result << 1) + (_bit ? 1 : 0);            
            return Convert.ToByte(_result);
        }

        public static byte NumberOfDays(this bool[] pBoolArray)
        {            
            byte result = 0;
            for (var i = 0; i < 7; i++)
            {
                if (pBoolArray[i])
                {
                    result++;
                }
            }
            return result;
        }

        public static byte HowManyInARow(this bool[] pBoolArray)
        {
            byte record = 0;
            byte result = 0;            

            for (int i = 0; i < 7; i++)
            {
                if (pBoolArray[i])
                {
                    result++;
                    continue;
                }
                // and if pBoolArray[i] == false
                if (result > record)
                {
                    record = result;
                    result = 0;
                }
            }
            if (result > record)
            {
                record = result;
            }
            return record;
        }

        public static bool[] WithBitOn(
            this bool[] pBoolArray,
            int pWhichBit)
        {
            var result = Engine.ArrayDeepCopy<bool>(pBoolArray);
            result[pWhichBit] = true; /* I don't care about out of range exception.
                                       * it is a method for "internal use only", it's
                                       * caller's responsibility to pass correct arguments */
            return result;
        }

        public static bool DoesWorkWholeWeekend(this bool[] pBoolArray)
        {            
            return pBoolArray[5] && pBoolArray[6]; // are two last bits ON?
        }

    }
}
